package com.nexgo.payment.trans.operator.edit;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 编辑操作员信息界面
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月7日
 */
public class OperatorEditActivity extends BaseActivity implements OperatorEditContract.View {
    private static final String TAG = OperatorEditActivity.class.getSimpleName();

    @BindView(R.id.tv_operator_no)
    TextView tvOperatorNo;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.et_confirm_password)
    EditText etConfirmPassword;

    private OperatorEditPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_operator);
        ButterKnife.bind(this);

        initTitle();

        Bundle bundle = getIntent().getExtras();
        String operatorNo = null;
        if (bundle != null){
            operatorNo = bundle.getString("operatorNo");
        }
        new OperatorEditPresenter(this, Repository.getInstance(), operatorNo);
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.confirm))
                .setTitleText(getString(R.string.operator_edit_title))
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        UIUtils.hideSoftKeyboard(this);
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                mPresenter.editOperator(etNewPassword.getText().toString().trim(), etConfirmPassword.getText().toString().trim());
                break;
            default:
                break;
        }
    }

    @Override
    public void setPresenter(OperatorEditPresenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showOperatorNo(String operatorNo) {
        tvOperatorNo.setText(operatorNo);
    }

    @Override
    public void showClearNewPassword() {
        etNewPassword.getText().clear();
    }

    @Override
    public void showClearConfirmPassword() {
        etConfirmPassword.getText().clear();
    }

    @Override
    public void showTips(String message) {
        ToastUtils.showToast(message);
    }

    @Override
    public void showSuccess() {
        Intent intent = new Intent();
        setResult(1000,intent);
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }
}
